The use of the following method calls are known to lead to slow querie:
ServiceManager.getImpedimentsForEntity(LogicalEntity inEntity);
Avoid executing DomainQueries that would hydrate a large number of entities (objects).
Avoid queries that can take a long time. This can deprive the rest of the N4 system of critical resources.
Your Groovy code is almost always called in a transaction. Avoid starting a transaction of your own as that may impact the outcome of the transaction started by N4.